package org.example.observer;

import java.util.ArrayList;
import java.util.List;

public class EnvironmentMonitor implements Subject {
    private List<Observer> observers;
    private EnvironmentData data;

    public EnvironmentMonitor() {
        observers = new ArrayList<>();
        data = new EnvironmentData();
    }

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(data);
        }
    }

    public void updateEnvironmentData(EnvironmentData newData) {
        this.data = newData;
        System.out.println("环境数据已更新: " + data);
        notifyObservers();
    }
}